package admin

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-vue-blog-sever/gla"
	"go-vue-blog-sever/models"
	"go-vue-blog-sever/utils"
	"strconv"
)

func GetBlog(c *gin.Context) {
	pages, err := strconv.Atoi(c.Query("page"))
	size, err := strconv.Atoi(c.Query("size"))
	if pages == 0 || size == 0 || err != nil {
		var er error
		if err != nil {
			er = err
		} else {
			er = errors.New("没有错误")
		}
		utils.LoggerInfo(gla.BindError, er.Error(), nil)
		utils.ResponseBad(c, gla.BindError, nil)
		return
	}
	var a models.BlogArticles
	var blog []models.BlogArticles
	offset := (pages - 1) * size
	var cnt int64
	if err = utils.MysqlDB.Table(a.TableName()).Count(&cnt).Limit(pages).Offset(offset).Find(&blog).Error; err != nil {
		utils.LoggerInfo("数据库查询失败", err.Error(), nil)
		utils.ResponseBad(c, gla.FindError, nil)
		return
	}

	for i, v := range blog {
		utils.MysqlDB.Table("blog_tags").Where("blog_id=?", v.ID).Scan(&blog[i].Tags)
		utils.MysqlDB.Table("users").Exec("select * from `users` where id=?", v.PublisherID).Select("username").Scan(&blog[i].PublisherName)
		utils.MysqlDB.Table("blog_types").Where("id=?", v.TypeID).Scan(&blog[i].BlogType)
		blog[i].PublisherTime = utils.FormatTime(blog[i].CreateAt)
		blog[i].TypeName = blog[i].BlogType.TypeName
		//fmt.Printf("blog tages is =%v\n", blog[i].Tags)
		for j := 0; j < len(blog[i].Tags); j++ {
			var t models.Tage
			utils.MysqlDB.Table("tage").Where("id=?", blog[i].Tags[j].TageID).First(&t)
			//fmt.Printf("tages tage id is %v\n", t)
			blog[i].TagList = append(blog[i].TagList, t.TagName)
		}

	}
	utils.ResponseSuccess(c, gla.FindSuccess, gin.H{
		"total": cnt,
		"blog":  blog,
	})
}

func GetCateType(c *gin.Context) {
	page, err := strconv.Atoi(c.Query("page"))
	size, err := strconv.Atoi(c.Query("size"))
	if size == 0 && size == page || err != nil {
		var er error
		if err != nil {
			er = err
		} else {
			er = errors.New("size and page =0")
		}
		utils.LoggerInfo(gla.BindError, er.Error(), nil)
		utils.ResponseBad(c, gla.BindError, nil)
		return
	}
	var cate []models.BlogTypes
	offset := (page - 1) * size
	var cnt int64
	if err = utils.MysqlDB.Table((&models.BlogTypes{}).TableName()).Count(&cnt).Limit(page).Offset(offset).Find(&cate).Error; err != nil {
		utils.LoggerInfo("查询失败", err.Error(), nil)
		utils.ResponseBad(c, gla.FindError, nil)
		return
	}
	//fmt.Printf("cate is %v\n", cate)
	for i := 0; i < len(cate); i++ {
		//fmt.Printf("createAt time=%v\n to string is %s", cate[i].CreatedAt, utils.FormatTime(cate[i].CreatedAt))
		cate[i].CreateTime = utils.FormatTime(cate[i].CreatedAt)
	}
	utils.ResponseSuccess(c, gla.FindSuccess, gin.H{
		"total": cnt,
		"cate":  cate,
	})

}

func GetAllCateType(c *gin.Context) {
	type CateName struct {
		Name string `json:"name" gorm:"column:type_name"`
	}
	var cname []CateName
	if err := utils.MysqlDB.Raw("select type_name from `blog_types`").Find(&cname).Error; err != nil {
		utils.LoggerInfo("查询失败", err.Error(), nil)
		utils.ResponseBad(c, gla.FindError, nil)
		return
	}
	utils.ResponseSuccess(c, "查询成功", cname)
}

func AddCategory(c *gin.Context) {

}
